package com.kh;

import com.esotericsoftware.tablelayout.Cell;
import com.esotericsoftware.tablelayout.swing.Table;
import com.formdev.flatlaf.FlatLightLaf;
import com.kh.chart.ChartHelper;
import com.kh.chart.DataHelper;
import com.kh.domain.ChannelData;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.xy.XYDataset;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class MainFrame extends JFrame {

    private List<ChannelData> channelDataList;

    private Table mainTable;
    private List<JPanel> panelList = new ArrayList<>();

    public MainFrame() {
        this.setTitle("DynamicPanel Demo");
        initFrame();
        this.setPreferredSize(new Dimension(800, 600));
        this.pack();
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void initFrame() {
        JPanel mainPanel = initMainPanel();
        JPanel commandPanel = initCommandPanel();
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(mainPanel, BorderLayout.CENTER);
        panel.add(commandPanel, BorderLayout.SOUTH);
        this.setContentPane(panel);
    }

    private JPanel initMainPanel() {
        mainTable = new Table();
        mainTable.debug();
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(mainTable, BorderLayout.CENTER);
        change2Mode1();

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(new JScrollPane(panel), BorderLayout.CENTER);
        mainPanel.setBorder(BorderFactory.createTitledBorder("Dynamic Panel"));
        return mainPanel;
    }

    private JPanel initCommandPanel() {
        JButton model1Button = new JButton("模式一");
        model1Button.addActionListener(e -> {
            change2Mode1();
        });
        JButton model2Button = new JButton("模式二");
        model2Button.addActionListener(e -> {
            change2Mode2();
        });
        JButton model3Button = new JButton("模式三");
        model3Button.addActionListener(e -> {
            change2Mode3();
        });
        JButton increaseHeightButton = new JButton("Increase");
        increaseHeightButton.addActionListener(e -> {
            increasePanelHeight();
        });

        Table panelTable = new Table();
        panelTable.pad(10);
        panelTable.addCell(model1Button);
        panelTable.addCell(model2Button);
        panelTable.addCell(model3Button);
        panelTable.addCell(increaseHeightButton);
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(panelTable, BorderLayout.CENTER);
        return panel;
    }

    private void change2Mode1() {
        List<ChannelData> channelDataList = getChannelDataList();
        final List<XYDataset> datasetList = ChartHelper.transFromMode1(channelDataList);
        updateChartPanel(datasetList);
    }

    private void change2Mode2() {
        List<ChannelData> channelDataList = getChannelDataList();
        final List<XYDataset> datasetList = ChartHelper.transFromMode2(channelDataList);
        updateChartPanel(datasetList);
    }

    private void change2Mode3() {
        List<ChannelData> channelDataList = getChannelDataList();
        final List<XYDataset> datasetList = ChartHelper.transFromMode3(channelDataList);
        updateChartPanel(datasetList);
    }

    private void updateChartPanel(List<XYDataset> datasetList) {
        CombinedDomainXYPlot combinedDomainXYPlot = new CombinedDomainXYPlot();
        combinedDomainXYPlot.setGap(0.0);
        for (XYDataset dataset : datasetList) {
            StandardXYItemRenderer renderer = new StandardXYItemRenderer();
            XYPlot plot = new XYPlot(dataset, null, new NumberAxis(), renderer);

            final ValueMarker valueMarker = ChartHelper.createSolidMarker(100.0);
            plot.addDomainMarker(valueMarker);

            final ValueMarker valueMarker1 = ChartHelper.createDashMarker(200.0);
            plot.addDomainMarker(valueMarker1);

            combinedDomainXYPlot.add(plot);
        }

        final ChartPanel chartPanel = ChartHelper.createChartPanel(combinedDomainXYPlot);
        mainTable.removeAll();
        mainTable.reset();
        mainTable.addCell(chartPanel).top().expand().fill();
        mainTable.layout();
    }

    private void increasePanelHeight() {
        List<Cell> cells = mainTable.getCells();
        for (Cell cell : cells) {
            float prefHeight = cell.getPrefHeight();
            cell.height(prefHeight * 1.5f);
        }
        mainTable.revalidate();
    }

    public List<ChannelData> getChannelDataList() {
        if (channelDataList == null) {
            channelDataList = DataHelper.createChannelDatas();
        }
        return channelDataList;
    }


    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(new FlatLightLaf());
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        EventQueue.invokeLater(() -> {
            MainFrame mainFrame = new MainFrame();
            mainFrame.setVisible(true);
        });
    }
}
